/*
* pps_bom
*/
package com.pitop.bom.service;

import com.pitop.bom.api.BomDetailFeignApi;
import com.pitop.bom.api.BomFeignApi;
import com.pitop.bom.entity.Bom;
import com.pitop.bom.entity.BomDetail;
import com.pitop.bom.entity.BomDetailState;
import com.pitop.bom.entity.BomState;
import com.pitop.bom.vo.BomDetailVO;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.BomDetailException;
import com.pitop.core.tools.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * bom明细
 *
 * @author berton
 */
@Service("bomDetailSVImpl")
@Slf4j
public class BomDetailSVImpl {

    @Autowired
    private BomDetailFeignApi bomDetailFeignApi;

    @Autowired
    private BomFeignApi bomFeignApi;

    /**
     * 加载一个对象BomDetail
     *
     * @param id * @param code 编码
     * @return BomDetail
     */
    public BomDetail load(Long id, String code) {
        if (id == null && code == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomDetailFeignApi.load(id, code);
    }

    /**
     * 加载一个对象BomDetail 通过id,(将查询关联数据)
     *
     * @param id
     * @return BomDetail
     */
    public BomDetail loadById(Long id) {
        if (id == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomDetailFeignApi.loadById(id);
    }

    /**
     * 加载一个对象BomDetail 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return BomDetail
     */
    public BomDetail loadByCode(String code) {
        if (code == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomDetailFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象BomDetail,(将查询关联数据)
     *
     * @param id * @param code 编码
     * @return BomDetail
     */
    public BomDetail get(Long id, String code) {
        if (id == null && code == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomDetailFeignApi.get(id, code);
    }

    /**
     * 加载一个对象BomDetail 通过id,(将查询关联数据)
     *
     * @param id
     * @return BomDetail
     */
    public BomDetail getById(Long id) {
        if (id == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomDetailFeignApi.getById(id);
    }

    /**
     * 加载一个对象BomDetail 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return BomDetail
     */
    public BomDetail getByCode(String code) {
        if (code == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return bomDetailFeignApi.getByCode(code);
    }

    /**
     * 删除对象BomDetail
     *
     * @param id * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        bomDetailFeignApi.delete(id, code);
    }

    /**
     * 查询BomDetail分页
     *
     * @param bomDetail 对象
     * @param curPage   当前页
     * @param pageSize  分页条数
     * @return List<BomDetail>
     */
    public List<BomDetail> list(BomDetail bomDetail, int curPage, int pageSize) {
        return bomDetailFeignApi.list(bomDetail, curPage, pageSize);
    }

    /**
     * 查询BomDetail分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<BomDetail>
     */
    public List<BomDetail> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        BomDetail bomDetail = new BomDetail();
        bomDetail.setId(id);
        bomDetail.setCode(code);

        return bomDetailFeignApi.list(bomDetail, curPage, pageSize);
    }

    public int count(Long id, String code) {
        return bomDetailFeignApi.count(id, code);
    }

    public int count(BomDetail bomDetail) {
        return bomDetailFeignApi.count(bomDetail);
    }

    /**
     * 保存
     *
     * @param bomDetail 实体
     * @param account
     * @param code      @throws BaseException
     */
    public void save(BomDetail bomDetail, String account, String code) {
        if (bomDetail == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        bomDetailFeignApi.build(bomDetail, account, code);
    }

    /**
     * 保存
     *
     * @param bomDetailVOs 实体
     * @param account
     * @param code         @throws BaseException
     */
    public void save(List<BomDetailVO> bomDetailVOs, String account, String code) {
        if (bomDetailVOs == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        BomDetailVO bomDetailVO = bomDetailVOs.get(0);
        Bom bom = bomFeignApi.loadByCode(bomDetailVO.getBomCode());
        if(bom == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        if(BomState.Develop != BomState.getEnum(bom.getState())) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Bom_Not_Develop);
        }
        bomDetailFeignApi.save(bomDetailVOs, account, code);
    }

    /**
     * 更新
     *
     * @param bomDetail 实体
     * @param account
     * @param code      @throws BaseException
     */
    public void modify(BomDetail bomDetail, String account, String code) {
        if (bomDetail == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        bomDetailFeignApi.modify(bomDetail, account, code);
    }

    /**
     * 根据箱体条件，统计产品数量
     *
     * @param code
     * @param state
     * @return
     */
    public BomDetail updateState(String code, String state) {
        if (StringTools.isEmpty(code) || StringTools.isEmpty(state)) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        BomDetail bomDetail = bomDetailFeignApi.loadByCode(code);
        if(bomDetail == null) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        if(BomDetailState.Develop != BomDetailState.getEnum(bomDetail.getState())) {
            throw new BomDetailException(BaseException.BaseExceptionEnum.Bom_Not_Develop);
        }
        return bomDetailFeignApi.updateState(code, state);
    }
}
